// FunkyDog MailBundle - tags and keywords for Apple Mail 3.0
//    Copyright (C) 2008 saptarshi guha saptarshi.guha@gmail.com
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

#import "MessageContentController-Extras.h"
#import "FunkyDog.h"
#import "TagPaneViewOwner.h"
#import "AppleMail/Library.h"
#import "AppleMail/Message.h"
#import <objc/objc.h>
#import <objc/objc-runtime.h>
#import <objc/objc-class.h>
@class Message;
@implementation  MessageContentController (Extras)
+(void)load{
	if (self == [MessageContentController class]){
		Method originalMethod ,replacedMethod;
		originalMethod  = class_getInstanceMethod(self, @selector(_updateDisplay));
		replacedMethod  = class_getInstanceMethod(self, @selector(fd_updateDisplay));
		method_exchangeImplementations(originalMethod, replacedMethod);
		originalMethod  = class_getInstanceMethod(self, @selector(fadeToEmpty));
		replacedMethod  = class_getInstanceMethod(self, @selector(myfadeToEmpty));
		method_exchangeImplementations(originalMethod, replacedMethod);
		NSLog(@"FunkySob goes Meeoow %@",[FunkySob sharedInstance]);
	}
}
-(void)myfadeToEmpty{
	[self fd_removeTagView];
	[self myfadeToEmpty];
}
-(void)fd_addTagView{
	NSView *maincontainer,*accessoryView;
	NSScrollView  *resizedView ; //= [contentContainerView fd_messageScroll] ;
	if([[contentContainerView subviews] count] >=1) resizedView=[[contentContainerView subviews] objectAtIndex:0]; //Assume Scrollview is first.
	maincontainer=[resizedView superview]; ;//[resizedView superview]; //or [contentContainerView contentView] 
	//should be same as //which should be same as contentContainerView;
	NSRect originalRect,aRect;
	float aHeight;
	TagPaneViewOwner *tp=[[TagPaneViewOwner alloc] initWithMessage:[[self message] path] 
															messId: [[self message] persistentID]  ];
	
	accessoryView=[tp  tagpane]; //[[FunkySob sharedInstance] tagpane];
	originalRect = aRect = [resizedView frame];
	aHeight = NSHeight([ accessoryView frame]);
	aRect.origin.y = NSHeight([maincontainer bounds]) + [ maincontainer bounds].origin.y - aHeight;
	originalRect.size.height =NSHeight([maincontainer frame])- aHeight;
	aRect.size.height = aHeight;
	[accessoryView setFrame:aRect];
	[maincontainer addSubview:accessoryView];
	[accessoryView setHidden:NO];
	[resizedView setFrame:originalRect];
	
}
-(void)fd_removeTagView{
	NSView *maincontainer;
//	NSScrollView  *resizedView =[contentContainerView subviews] object [contentContainerView fd_messageScroll] ;
	NSScrollView *resizedView;
	if([[contentContainerView subviews] count] >=1) 
	 {resizedView=[[contentContainerView subviews] objectAtIndex:0];
	
	maincontainer=[resizedView superview]; //which should be same as contentContainerView
	NSRect r=[resizedView frame];
	r.size.height=NSHeight([maincontainer frame]);
	[ resizedView setFrame:r];
	if ([ [maincontainer subviews] count]>1) {
		[ [[maincontainer subviews] objectAtIndex:1] setHidden:YES];
		[[ [maincontainer subviews] objectAtIndex:1] removeFromSuperviewWithoutNeedingDisplay]; //me!removeFromSuperviewWithoutNeedingDisplay	
	 }}
}	
-(void)fd_updateDisplay{
	if( [self message] &&
	   [[self message] isKindOfClass:NSClassFromString(@"LibraryMessage")] &&
	   ([[self message] type]==MAILTYPE || [[self message] type] ==RSSTYPE)
	   		){
		[self fd_addTagView];
	//	[self fddisplayDetails];
	}else{
		[self fd_removeTagView];
		}	
//	NSLog(@"Type view %@",contentContainerView);
	[self fd_updateDisplay];
}
@end
